/*
 * Copyright 2011 CraneLeeon authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crane.webtemplate.action;


import org.crane.webtemplate.exception.TextMessageException;
import org.crane.webtemplate.dao.security.UserDAO;
import org.crane.webtemplate.model.security.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.exception.ConstraintViolationException;
import org.json.JSONObject;
import static org.crane.webtemplate.Tokens.*;

/**
 *
 * @author CraneLeeon
 */
@Action("/Auth")
@Results({
    @Result(name = "page", type = "dispatcher", location = "Auth.jsp"),
    @Result(name = "json", type = "dispatcher", location = "/WEB-INF/jsp/json.jsp"),
    @Result(name = "xml", type = "dispatcher", location = "/WEB-INF/jsp/xml.jsp")
})
public class Auth extends BaseAction{

    protected static final Log logger = LogFactory.getLog(Auth.class);
    private String name;
    private String password;

    @Override
    public Class getBoClass() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String execute() throws Exception {
        try {
            switch (this.act) {
                case ACT_LOGIN: login();break;

            }
        } catch (TextMessageException ex) {
            this.json = new JSONObject().put("success", false).put("info", getText(ex.getMessage())).toString();
        } catch (ConstraintViolationException ex) {
            logger.debug(ex);
            this.json = new JSONObject().put("success", false).put("info", getText("ConstraintViolationException")).toString();
        } catch (Exception ex) {
            logger.debug(ex);
            this.json = new JSONObject().put("success", false).put("info", ex).put("err_code", ERR_UNKNOWN).toString();
        }
        return "json";
    }

    private void login() throws Exception{
        JSONObject jobj = new JSONObject();
        UserDAO dao = new UserDAO();
        User user = (User) dao.getUserByName(name);
        if(user != null){
            if(this.name != null && this.password != null && this.name.equals(user.getName()) && this.password.equals(user.getPassword())){
                this.session.put("user", user);
                jobj.put("success", true)
                        .put("info", "");
            }else{
                jobj.put("success", false)
                        .put("info", this.getText("Wrong password or user name"));

            }
        }else{
                jobj.put("success", false)
                        .put("info", this.getText("No matched user name"));

        }
        this.json = jobj.toString();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    

}
